package org.shop.controller.app.live;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.shop.entity.AnswerInfo;
import org.shop.entity.security.MyUserDetail;
import org.shop.service.AnswerInfoService;
import org.shop.service.PraiseService;
import org.shop.util.CommonUtil;
import org.shop.util.ObjUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class AnswerContrller {
	private static Logger logger = LogManager.getLogger(AnswerContrller.class);
	
	@Autowired
	private AnswerInfoService answerInfoService;
	@Autowired
	private PraiseService praiseService;
	
    /**
     * app论坛留言回复
     * @param req
     * @return
     */
    @RequestMapping("app/addPostReply")
    @ResponseBody
    public Map<String, Object> addPostReply(HttpServletRequest req){
        Map<String, Object> map = new HashMap<String, Object>();
        try{
        	String postId = req.getParameter("postId");
        	String receiverId = req.getParameter("receiverId");
        	String receiverName = req.getParameter("receiverName");
        	String receiverHeadImg = req.getParameter("receiverHeadImg");
            String content = req.getParameter("content");
            String textBlackList=CommonUtil.getPropertiesParam("textBlackList");
            for(String textBlack:textBlackList.split(",")){
                if(content.contains(textBlack)){
                    map.put("state", 2);
                    map.put("msg", "发布内容不合法");
                    return map;
                }
            }
            MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            AnswerInfo answerInfo = new AnswerInfo();
            answerInfo.setPostId(Long.parseLong(postId));
            answerInfo.setContent(content);
            answerInfo.setSenderId(ud.getUserId());
            answerInfo.setSendName(ud.getUserName());
            answerInfo.setSendHeadImg(ud.getUserPhoto());
            if(ObjUtil.isNotNullAndEmpty(receiverId)) {
            	answerInfo.setReceiverId(Long.parseLong(receiverId));
            	answerInfo.setReceiverName(receiverName);
            	answerInfo.setReceiverHeadImg(receiverHeadImg);
            }
            answerInfoService.insertSelective(answerInfo);
            map.put("state", 1);
            map.put("msg", "发布成功");
        }catch(Exception e){
            e.printStackTrace();
            logger.error(e);
            map.put("state", 0);
            map.put("msg", "发布失败");
        }
        return map;
    }
    
    /**
     * app根据id获取热门评论列表
     * @param req
     * @return
     */
    @RequestMapping("app/getPostHotAnswerList")
    @ResponseBody
    public List<Map<String, Object>> getPostHotAnswerList(HttpServletRequest req){
    	SecurityContextImpl securityContextImpl = (SecurityContextImpl)req.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
        MyUserDetail ud = null;
        if(ObjUtil.isNotNullAndEmpty(securityContextImpl)) {
        	ud = (MyUserDetail) securityContextImpl.getAuthentication().getPrincipal();
        }
        String postId = req.getParameter("postId");
		String currentPage = req.getParameter("currentPage");
		String pageSize = req.getParameter("pageSize");
    	Map<String, Object> params = new HashMap<String, Object>();
    	params.put("postId", postId);
    	if(ObjUtil.isNotNullAndEmpty(ud)) {
    		params.put("userId", ud.getUserId());
    	}
    	params.put("currentPage", (Integer.parseInt(currentPage) - 1) * Integer.parseInt(pageSize));
		params.put("pageSize", pageSize);
    	List<Map<String, Object>> getPostHotAnswerList = answerInfoService.getPostHotAnswerList(params);
//    	List<Map<String, Object>> getPostHotAnswerList2 = new ArrayList<Map<String,Object>>();
//    	for(int i=0;i<getPostHotAnswerList.size();i++) {
//    		Map<String, Object> map = getPostHotAnswerList2.get(i);
//    		Map<String, Object> m = new HashMap<String, Object>();
//    		m.put("targetId", map.get("answerId"));
//    		map.put("praiseCount", praiseService.getPraiseCount(m));
//    		getPostHotAnswerList2.add(map);
//    	}
        return getPostHotAnswerList;
    }
    
    /**
     * app根据id获取评论列表
     * @param req
     * @return
     */
    @RequestMapping("app/getAnswerList")
    @ResponseBody
    public List<Map<String, Object>> getAnswerList(HttpServletRequest req){
    	SecurityContextImpl securityContextImpl = (SecurityContextImpl)req.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
        MyUserDetail ud = null;
        if(ObjUtil.isNotNullAndEmpty(securityContextImpl)) {
        	ud = (MyUserDetail) securityContextImpl.getAuthentication().getPrincipal();
        }
        String postId = req.getParameter("postId");
		String currentPage = req.getParameter("currentPage");
		String pageSize = req.getParameter("pageSize");
    	Map<String, Object> params = new HashMap<String, Object>();
    	params.put("postId", postId);
    	if(ObjUtil.isNotNullAndEmpty(ud)) {
    		params.put("userId", ud.getUserId());
    	}
    	params.put("currentPage", (Integer.parseInt(currentPage) - 1) * Integer.parseInt(pageSize));
		params.put("pageSize", pageSize);
    	List<Map<String, Object>> postAnswerList = answerInfoService.getPostAnswerList(params);
//    	List<Map<String, Object>> postAnswerList2 = new ArrayList<Map<String,Object>>();
//    	for(int i=0;i<postAnswerList.size();i++) {
//    		Map<String, Object> map = postAnswerList.get(i);
//    		Map<String, Object> m = new HashMap<String, Object>();
//    		m.put("targetId", map.get("answerId"));
//    		map.put("praiseCount", praiseService.getPraiseCount(m));
//    		postAnswerList2.add(map);
//    	}
        return postAnswerList;
    }
    
    /**
     * 删除评论
     * @param req
     * @return
     */
    @RequestMapping("app/delAnswer")
    @ResponseBody
    public Map<String, Object> delAnswer(HttpServletRequest req){
        Map<String, Object> map = new HashMap<String, Object>();
        String answerId = req.getParameter("answerId");
        try {
        	answerInfoService.deleteByPrimaryKey(Long.parseLong(answerId));
            map.put("state", 1);
            map.put("msg", "删除成功");
        } catch(Exception e) {
            e.printStackTrace();
            logger.error(e);
            map.put("state", 0);
            map.put("msg", "删除失败");
        }
        return map;
    }
}
